$(function(){
	$("#back").click(function(){
		if($("#shareContent").val()||$(".share_upfile img").size()>0){
			$.confirm("确定不发布了嘛？", function(){
				$.go(activityUrl)
			});
		}else{
			$.go(activityUrl)
		}
	});
	var uploader = WebUploader.create({
		auto: true,
		server: upImageUrl,
		pick: {id:'.share_upfile',multiple:false},
		fileVal: "upfile",
		threads: 3,
		accept: {
			title: 'Images',
			extensions: 'gif,jpg,jpeg,bmp,png',
			mimeTypes: 'image/*'
		},
		resize: false,
		compress: {
			width: 600,
			height: 600,
			quality: 70,
			allowMagnify: false,
			crop: false,
			preserveHeaders: true,
			noCompressIfLarger: false,
			compressSize: 0
		}
	});
	//图片加入队列
	uploader.on("fileQueued", function(file){
		$(".share_upfile.active").removeClass("add").addClass("up");
	})
	//添加图片
	uploader.on("uploadSuccess", function(file, data){
		if(data.flag){
			uploader.makeThumb(file, function(error, src){
				$(".share_upfile.active").append('<img src="'+src+'">').removeClass("up").addClass("del").find("input").data({"id":data.id,"file":file});
			},200, 200);
		}else{
			$(".share_upfile.active").removeClass("up").addClass("add");
			$.tips(data.state);
		}
	});
	$(".share_upfile").on("touchstart mousedown", function(){
		$(this).addClass("active").siblings().removeClass("active")
	});
	//uploader错误信息
	uploader.on("error", function(data){
		if(data=="F_DUPLICATE"){
			$.tips("请勿上传重复的图片")
		}else{
			$.tips("上传失败，请重新上传")
		}
		
	});
	//删除图片
	$(".share_images").on("click", ".share_upfile.del", function(){
		var input = $(this).find("input");
		uploader.removeFile(input.data("file"));
		$(this).removeClass("del").addClass("add").children("img").remove();
		input.removeData();
		
	});
	//参与活动
	$(".share_submit").click(function(){
		var content = $.trim($("#shareContent").val());
		var images = $(".share_upfile").map(function(){return $(this).find("input").data("id");}).get().join(",");
		if(content==""){
			$.tips("请输入你想说的话");
			return false;
		}else if(content.length>500){
			$.tips("内容不能超过500个字");
			return false;
		}else if(images==""){
			$.tips("请上传图片！");
		}else{
			$.ajax({
				url: joinUrl,
				type: "post",
				data: {"content":content,"act_id":actId,"c_image":images},
				dataType: 'json',
				success: function(data){
					if(data.flag){
						$.tips(data.msg);
						$.go(activityUrl);
					}else{
						$.tips(data.msg);
						return false;
					}
				},
				error: function(){
					$.tips("网络不给力，上传失败");
				}
			});
		}	
	});
	
})